﻿//===================================================================================
// $$
//
//
// Code By: Code Milker V2
// Date On: 2013//
//===================================================================================


namespace Forest.Lion.Data.SqlServer.Repositories
{
    using System;

    using System.Collections.Generic;

    using System.Data;

    using System.Linq;
    using Forest.Lion.Model;

    public class TrainingRepository : BaseRepository, ITrainingRepository
    {
        public TrainingRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }
        public void Create(Training training)
        {
            this.GetDbSet<Training>().Add(training);
            this.UnitOfWork.SaveChanges();
        }

        public Training GetTraining(Guid trainingId)
        {
            return this.GetDbSet<Training>()
                .Where(c => c.Id == trainingId)
                .Single();
        }

        public void Update(Training updatedTraining)
        {
            this.GetDbSet<Training>().Attach(updatedTraining);
            this.SetEntityState(updatedTraining, updatedTraining.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid trainingId)
        {
            Training trainingToDelete = this.GetTraining(trainingId);
            this.GetDbSet<Training>().Remove(trainingToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IEnumerable<Training> Find()
        {
            return this.GetDbSet<Training>().Take(100)
                .ToList();
        }

        public IEnumerable<Training> GetAll()
        {
            return this.GetDbSet<Training>().Take(100)
                .ToList();
        }
    }
}
